<?php
namespace app\common\service\api\service;
use app\common\service\api\interfaces\DictionaryApi;
use app\common\exception\TokenException;
use app\common\exception\LogicException;
use think\db\exception\DataNotFoundException;
class DictionaryApiService implements DictionaryApi{
    public function getDictionaryListPage(){
        $where[] = ['dictionary_id', '>', 0];
        $list= app('dictionary_ability')->getListPage($where);
        if ($list->isEmpty()) {
            throw new DataNotFoundException(lang('DATA_NOT_FOUND'));
        } else {
            $data['total'] =app('dictionary_ability')->getCount($where);
            $data['list'] = $list;
            return $data;
        }
        return $requestData;
    }
    public function saveDictionaryAdd(){
        $requestData = input();
        $requestData['status'] = 1;
        $result=app('dictionary_ability')->saveAdd($requestData);
        if ($result !== false) {
            return $result;
        } else {
            throw new LogicException(lang('SAVE_ERROR'));
        }
    }
    public function getDictionaryRow(){
        $requestData = input();
        $where[] = ['dictionary_id', '=', $requestData['dictionary_id']];
        $row =app('dictionary_ability')->getLine($where);
        if (empty($row)) {
            throw new DataNotFoundException(lang('DATA_NOT_FOUND'), 10006);
        } else {
            return $row;
        }
    }
    public function saveDictionaryEdit(){
        $this->getDictionaryRow();
        $requestData = input();
        $result =app('dictionary_ability')->saveEdit($requestData);;
        if ($result !== false) {
            return $result;
        } else {
            throw new LogicException(lang('SAVE_ERROR'));
        }
    }
    public function saveDictionaryDel(){
        $this->getDictionaryRow();
        $requestData = input();
        $result =app('dictionary_ability')->saveDel($requestData);;
        if ($result !== false) {
            return $result;
        } else {
            throw new LogicException(lang('SAVE_ERROR'));
        }
    }
    public function getTag(){
        $requestData = input();
        $where[]=['name_en', '=', $requestData['name_en']];
        $dictionaryRow =app('dictionary_ability')->getLine($where);
        if (empty($dictionaryRow)) {
            throw new DataNotFoundException(lang('DATA_NOT_FOUND'));
        } else {
            return app('dictionary_value_ability')->getDictionaryValue($dictionaryRow['dictionary_id']);
        }
    }
}